import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from sklearn.metrics import calinski_harabasz_score

x, y = make_blobs(n_samples=500,
                  n_features=2,
                  centers=[[-1, -1], [0, 0], [1, 1], [2, 2]],
                  cluster_std=[0.4, 0.2, 0.2, 0.3],
                  random_state=22
                  )
score = []
for k in range(1,20):
    model = KMeans(n_clusters=k)  # 创建模型
    model.fit(x)
    score.append(model.inertia_)

plt.figure(figsize=(20,10),dpi=80)
plt.plot(range(1,20),score,marker='o')
plt.show()